/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.Map;

import fever.commands.JSCommand;
import fever.commands.JSMethodName;
import fever.log.FeverDebug;
import fever.utils.Stringifier;

import flash.external.ExternalInterface;

/**
 * Control and manage browser iframe.
 * 
 * <p>Sets the <strong>wmode</strong> to <strong>opaque</strong> 
 * to play properly with iFrame with non IE browser family( FireFox, ... ).
 *
 * @author Romain Ecarnot
 */
class fever.app.browser.BrowserFrame
{
	//-------------------------------------------------------------------------
	// Constants definition
	//-------------------------------------------------------------------------
	
	/** Scrolling bar if needed. Default. */
	public static var SCROLLING_AUTO : String = 'auto';
	
	/** Scrolling bar always. */
	public static var SCROLLING_ALWAYS : String = 'yes';
	
	/** Scrolling bar never. */
	public static var SCROLLING_NEVER : String = 'no';
	
	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _m : Map;
	private static var _createMethod : JSCommand;
	private static var _createIFrameMETHOD : JSMethodName;
	private static var _bInit : Boolean = false;
	
	private var _id : String;		private var _url : String;	private var _x : Number;	private var _y : Number;	private var _width : Number;	private var _height : Number;
	private var _scrolling : String;	private var _border : Boolean;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** IFrame's position on X-Axis. */
	public function get x() : Number { return _x; }
	public function set x( n : Number ) { move( n, null ); }
	
	/** IFrame's position on Y-Axis. */
	public function get y() : Number { return _y; }
	public function set y( n : Number ) { move( null, n ); }
	
	/** IFrame's width. */
	public function get width() : Number { return _width; }
	public function set width( w : Number ) { setSize( w, null ); }
	
	/** IFrame's height. */
	public function get height() : Number { return _height; }
	public function set height( h : Number ) { setSize( null, h ); }
	
	/** Indicates the IFrame scrolling status. */
	public function get scrolling() : String { return _scrolling; }
	public function set scrolling ( type : String ) : Void 
	{ 
		if( type != _scrolling )
		{
			_scrolling = type;
			_callInterface( "scrolling='" + _scrolling + "'" );
		}
	}
	
	/** Indicates the IFrame border status. */
	public function get enabledBorder() : Boolean { return _border; }
	public function set enabledBorder ( b : Boolean ) : Void 
	{ 
		if( b != _border )
		{
			_border = b;
			_callInterface( "frameBorder='" + ( ( b ) ? 1 : 0 ) + "'" );
		}
	}
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns the popup registred with {@code frameID} name.
	 * 
	 * <p>If <code>frameID</code> does not exist, frame is created.
	 */
	public static function get( frameID : String ) : BrowserFrame
	{
		if( _m.containsKey( frameID ) ) return _m.get( frameID );
		else
		{
			if( !_bInit ) _init();
			return new BrowserFrame( frameID );
		}
	}
	
	/**
	 * Disposes popup registred with {@code frameID} name.
	 * 
	 * <p>Closes the popup and unregisters it from {@link BrowserFrame} 
	 * manager.
	 */
	public function dispose( frameID : String ) : Void
	{
		if( _m.containsKey( frameID ) ) 
		{
			_m.get( frameID ).close();
			_m.remove( frameID );
		}
		else FeverDebug.ERROR( frameID + ' frame is not registred' );
	}
	
	/**
	 * Returns window's id.
	 */
	public function getID(  ) : String
	{
		return _id;	
	}
	
	/**
	 * Sets frame content.
	 */
	public function setURL( url : String ) : Void
	{
		if( url && ( url != _url ) )
		{
			_url = url;
			_callInterface( "src='" + _url + "'" );
		}
	}
	
	/**
	 * Returns correct frame content.
	 */
	public function getURL( ) : String
	{
		return String( ExternalInterface.call( 'eval', _getIFrameAccessor() + '.src;' ) );	
	}
	
	/**
	 * Opens popup
	 */
	public function open() : Void
	{
		_callInterface( "style.display=''" );
	}
	
	/**
	 * Closes the popup.
	 * 
	 * <p>Stay registred in manager.
	 */
	public function close() : Void
	{
		_callInterface( "style.display='none'" );
	}
	
	/**
	 * Moves the popup to {@code x, y} coordinates.
	 */
	public function move( x : Number, y : Number ) : Void
	{
		if( x && ( x  != _x ) ) 
		{
			_x = x;
			_callInterface( "style.left='" + x + "px'" );	
		}
		
		if( y && ( y  != _y ) ) 
		{
			_y = y;
			_callInterface( "style.top='" + y + "px'" );	
		}
	}
	
	/**
	 * Resizes the popup frame to {@code width}x{@code height} size.
	 */
	public function setSize( width : Number, height : Number ) : Void
	{
		if( width && ( width  != _width ) ) 
		{
			_width = width;
			_callInterface( "width='" + width + "'" );
		}
		
		if( height && ( height  != _height ) ) 
		{
			_height = height;
			_callInterface( "height='" + height + "'" );
		}
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * 
	 */
	private static function _init(  ) : Void
	{
		_m = new Map();
		
		_createIFrameMETHOD = new JSMethodName( 'createIFrame' );
		
		var a : String = "var aFvFrames = new Array();";
		ExternalInterface.call( "eval", a );
		
		var cm : String = "";
		cm += "( id ){";
		cm += "	var f = document.createElement('iframe');";
		cm += "	f.id = id;";
		cm += "	f.style.position = 'absolute';";
		cm += "	f.style.display = 'none';";		cm += "	f.style.top = '0px';";
		cm += "	f.style.left = '0px';";
		cm += "	f.width = 0;";		cm += "	f.height = 0;";		cm += "	document.body.appendChild( f );";
		cm += "	aFvFrames[id] = f;";
		cm += "};";
		
		_createMethod = new JSCommand( _createIFrameMETHOD, cm );
		
		_bInit = true;
	}
	
	private function BrowserFrame( frameID : String )
	{
		_id = frameID;
		
		_x = 0;
		_y = 0;
		_width = 0;
		_height = 0;
		_scrolling = SCROLLING_AUTO;
		_border = true;
		
		_createMethod.call( _id );
		
		_m.put( _id, this );
	}
	
	private function _getIFrameAccessor() : String
	{
		return "aFvFrames['" + _id + "']";	
	}
	
	private function _callInterface( arguments : String ) : Void
	{
		ExternalInterface.call( 'eval', _getIFrameAccessor() + '.' + arguments + ';' );	
	}
}